package com.example.queryexpressdelivery_01.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.queryexpressdelivery_01.entity.AdminData;
import com.example.queryexpressdelivery_01.common.DeliveryDAL;
import com.example.queryexpressdelivery_01.R;
import com.example.queryexpressdelivery_01.entity.UserData;

import java.util.List;

public class RegisterActivity extends AppCompatActivity {

    private Button btnRegisterRegister;
    private TextView tvRegisterLogin;
    private EditText edtRegisterAccount;
    private EditText edtRegisterPhone;
    private EditText edtRegisterPassword;
    private EditText edtRegisterPassword1;
    private EditText edtRegisterEmail;

    private DeliveryDAL deliveryDAL = new DeliveryDAL(this);
    private List<AdminData> adminDataList =deliveryDAL.queryAdminAll();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_register);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        initView();

        btnRegisterRegister.setOnClickListener(new View.OnClickListener() {     //点击注册按钮，开始处理
            @Override
            public void onClick(View view) {
                String account = edtRegisterAccount.getText().toString().trim();
                String phone = edtRegisterPhone.getText().toString().trim();
                String password = edtRegisterPassword.getText().toString().trim();
                String password1 = edtRegisterPassword1.getText().toString().trim();
                String email = edtRegisterEmail.getText().toString().trim();
                if (!account.isEmpty() && !phone.isEmpty() && !password.isEmpty() && !password1.isEmpty()){
                    if (password.equals(password1)){
                        List<UserData> userDataList =deliveryDAL.queryUserAll();
                        boolean isExisting = false;
                        for (UserData user:userDataList){       // 判断是否有重复的用户账号
                            if (account.equals(user.getAccount())){
                                isExisting=true;
                            }
                        }
                        for (AdminData admin:adminDataList){       //   判断是否与管理员账号冲突
                            if (account.equals(admin.getAccount())){
                                isExisting=true;
                            }
                        }
                        if (!isExisting) {      // 如果没有冲突，就插入到数据库并作出对应的提示
                            UserData user = new UserData(account,phone,password,email,null,null,"0");
                            long result = deliveryDAL.insertUser(user);
                            if (result != 0){
                                Log.i("TAG","点击第一个");
                                View v = getLayoutInflater().inflate(R.layout.dialog_register,null);
                                new AlertDialog.Builder(RegisterActivity.this).setView(v).setTitle("注册结果")
                                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialogInterface, int i) {
                                                dialogInterface.dismiss();
                                            }
                                        }).create().show();
                            }else Toast.makeText(RegisterActivity.this,"发生未知错误！",Toast.LENGTH_SHORT).show();
                        }else Toast.makeText(RegisterActivity.this,"账号已存在！",Toast.LENGTH_SHORT).show();
                    }else Toast.makeText(RegisterActivity.this,"两次输入密码不一致！",Toast.LENGTH_SHORT).show();
                }else Toast.makeText(RegisterActivity.this,"必填项未填写！",Toast.LENGTH_SHORT).show();
            }
        });


        tvRegisterLogin.setOnClickListener(new View.OnClickListener() {     // 点击返回
            @Override
            public void onClick(View view) {
                startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                finish();
            }
        });

    }

    private void initView() {
        btnRegisterRegister = (Button) findViewById(R.id.btnRegisterRegister);
        tvRegisterLogin = (TextView) findViewById(R.id.tvRegisterLogin);
        edtRegisterAccount = (EditText) findViewById(R.id.edtRegisterAccount);
        edtRegisterPhone = (EditText) findViewById(R.id.edtRegisterPhone);
        edtRegisterPassword = (EditText) findViewById(R.id.edtRegisterPassword);
        edtRegisterPassword1 = (EditText) findViewById(R.id.edtRegisterPassword1);
        edtRegisterEmail = (EditText) findViewById(R.id.edtRegisterEmail);
    }
}